package com.jhgsys.internal.workflow.service.impl;

import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.workflow.entity.WorkflowNodeTransCondition;
import com.jhgsys.internal.workflow.mapper.WorkflowNodeTransConditionMapper;
import com.jhgsys.internal.workflow.service.IWorkflowNodeTransConditionService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 流程节点流向条件 Service实现
 *
 * @author fenghaijun
 * @date 2024-07-19 09:56:38
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class WorkflowNodeTransConditionServiceImpl extends ServiceImpl<WorkflowNodeTransConditionMapper, WorkflowNodeTransCondition> implements IWorkflowNodeTransConditionService {

    /**
     * 查询（分页）
     * @author fenghaijun
     * @date 2024-07-19 09:56:38
     * @param request QueryRequest
     * @param workflowNodeTransCondition workflowNodeTransCondition
     * @return IPage<WorkflowNodeTransCondition>
    */
    @Override
    public IPage<WorkflowNodeTransCondition> findWorkflowNodeTransConditions(QueryRequest request, WorkflowNodeTransCondition workflowNodeTransCondition) {
        LambdaQueryWrapper<WorkflowNodeTransCondition> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkflowNodeTransCondition::getDeletedFlg, FebsConstant.VALID);
        // TODO 设置查询条件

        Page<WorkflowNodeTransCondition> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author fenghaijun
     * @date 2024-07-19 09:56:38
     * @param workflowNodeTransCondition workflowNodeTransCondition
     * @return List<WorkflowNodeTransCondition>
     */
    @Override
    public List<WorkflowNodeTransCondition> findWorkflowNodeTransConditions(WorkflowNodeTransCondition workflowNodeTransCondition) {
	    LambdaQueryWrapper<WorkflowNodeTransCondition> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkflowNodeTransCondition::getDeletedFlg, FebsConstant.VALID);
        if(null!=workflowNodeTransCondition.getTenantId()){
            queryWrapper.eq(WorkflowNodeTransCondition:: getTenantId, workflowNodeTransCondition.getTenantId());
        }
        if(null!=workflowNodeTransCondition.getWorkflowId()){
            queryWrapper.eq(WorkflowNodeTransCondition:: getWorkflowId, workflowNodeTransCondition.getWorkflowId());
        }
        if(null!=workflowNodeTransCondition.getWorkflowNodeTransId()){
            queryWrapper.eq(WorkflowNodeTransCondition:: getWorkflowNodeTransId, workflowNodeTransCondition.getWorkflowNodeTransId());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author fenghaijun
    * @date 2024-07-19 09:56:38
    * @param id
    * @return WorkflowNodeTransCondition
    */
    @Override
    public WorkflowNodeTransCondition findWorkflowNodeTransConditionById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author fenghaijun
     * @date 2024-07-19 09:56:38
     * @param workflowNodeTransCondition workflowNodeTransCondition
     */
    @Override
    @Transactional
    public void createWorkflowNodeTransCondition(WorkflowNodeTransCondition workflowNodeTransCondition) {
        workflowNodeTransCondition.setCreatedTime(new Date());
        this.save(workflowNodeTransCondition);
    }

    /**
     * 修改
     * @author fenghaijun
     * @date 2024-07-19 09:56:38
     * @param workflowNodeTransCondition workflowNodeTransCondition
     */
    @Override
    @Transactional
    public void updateWorkflowNodeTransCondition(WorkflowNodeTransCondition workflowNodeTransCondition) {
        workflowNodeTransCondition.setUpdatedTime(new Date());
        this.saveOrUpdate(workflowNodeTransCondition);
    }

    /**
     * 逻辑删除
     * @author fenghaijun
     * @date 2024-07-19 09:56:38
     * @param workflowNodeTransCondition workflowNodeTransCondition
     * @param workflowNodeTransConditionIds workflowNodeTransConditionIds
     */
    @Override
    @Transactional
    public void deleteWorkflowNodeTransCondition(WorkflowNodeTransCondition workflowNodeTransCondition,String workflowNodeTransConditionIds) {
        List<String> list = Arrays.asList(workflowNodeTransConditionIds.split(StringPool.COMMA));
        this.baseMapper.update(workflowNodeTransCondition,new LambdaQueryWrapper<WorkflowNodeTransCondition>().in(WorkflowNodeTransCondition::getWorkflowNodeTransConditionId,list));
	}

    /**
    * 物理删除
    * @author fenghaijun
    * @date 2024-07-19 09:56:38
    * @param workflowNodeTransConditionIds workflowNodeTransConditionIds
    */
    @Override
    @Transactional
    public void physicallyDeleteWorkflowNodeTransCondition(String workflowNodeTransConditionIds) {
        List<String> list = Arrays.asList(workflowNodeTransConditionIds.split(StringPool.COMMA));
        LambdaQueryWrapper<WorkflowNodeTransCondition> wapper = new LambdaQueryWrapper<>();
        wapper.in(WorkflowNodeTransCondition::getWorkflowNodeTransConditionId,list);
        this.remove(wapper);
    }
}
